﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YGame.Base;
using System.Diagnostics;

namespace YGame.GameServer
{
	/// <summary>
	/// 主模块
	/// 这里负责加载各个子模块
	/// </summary>
	public class MailModel: IMainModel
	{
		IAppFrameService AppFrameService;
		IGameServerProvider GameProvider;
		public void Install(IAppFrameService appFrameService)
		{
			Trace.WriteLine(string.Format("model Install: {0}", this.GetType().FullName));
			AppFrameService = appFrameService;
			appFrameService.Shutdown += new EventHandler(appFrameService_Shutdown);

			// 初始化网关连接
			YGame.Base.ServerGlobal.GatewayConnect = new GatewayConnect();
			YGame.Base.ServerGlobal.GatewayConnect.Start();

			// GameServerProvider 依赖于网关
			GameProvider = new GameServerProvider();
			GameProvider.Start();
			YGame.Base.ServerGlobal.Create(GameProvider);

			// 初始化 Game Logic
			YGame.Base.ServerGlobal.GameLogicProvider = new YGame.GameLogic.GameLogicProvider();
			YGame.Base.ServerGlobal.GameLogicProvider.Install(GameProvider);



		}

		public void Uninstall()
		{
			Trace.WriteLine(string.Format("model uninstall: {0}", this.GetType().FullName));

			YGame.Base.ServerGlobal.GameLogicProvider.Uninstall();
			YGame.Base.ServerGlobal.GatewayConnect.Stop();
		}


		void appFrameService_Shutdown(object sender, EventArgs e)
		{
			OnClose();
		}

		/// <summary>
		/// 卸载各个模块
		/// </summary>
		private void OnClose()
		{
			
		}

	}

}
